﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.Design.WebControls;
using System.Web.UI.Design;

namespace SPL.Web.UI
{
    /// <summary>
    /// A designer class for a strongly typed repeater <c>KvtRepeater</c>
    /// </summary>
	public class KvtRepeaterDesigner : System.Web.UI.Design.ControlDesigner
	{
	    /// <summary>
        /// Initializes a new instance of the <see cref="T:KvtRepeaterDesigner"/> class.
        /// </summary>
		public KvtRepeaterDesigner()
		{
		}

        /// <summary>
        /// Initializes the control designer and loads the specified component.
        /// </summary>
        /// <param name="component">The control being designed.</param>
		public override void Initialize(IComponent component)
		{
			if (!(component is KvtRepeater))
			{ 
				throw new ArgumentException("Component is not a KvtRepeater."); 
			} 
			base.Initialize(component); 
			base.SetViewFlags(ViewFlags.TemplateEditing, true); 
		}


		/// <summary>
		/// Generate HTML for the designer
		/// </summary>
		/// <returns>a string of design time HTML</returns>
		public override string GetDesignTimeHtml()
		{

			// Get the instance this designer applies to
			//
			KvtRepeater z = (KvtRepeater)Component;
			z.DataBind();

			return base.GetDesignTimeHtml();

			//return z.RenderAtDesignTime();

			//	ControlCollection c = z.Controls;
			//Totem z = (Totem) Component;
			//Totem z = (Totem) Component;
			//return ("<div style='border: 1px gray dotted; background-color: lightgray'><b>TagStat :</b> zae |  qsdds</div>");

		}
	}

    /// <summary>
    /// A strongly typed repeater control for the <c cref="KvtRepeater"></c> Type.
    /// </summary>
	[Designer(typeof(KvtRepeaterDesigner))]
	[ParseChildren(true)]
	[ToolboxData("<{0}:KvtRepeater runat=\"server\"></{0}:KvtRepeater>")]
	public class KvtRepeater : Control, System.Web.UI.INamingContainer
	{
	    /// <summary>
        /// Initializes a new instance of the <see cref="T:KvtRepeater"/> class.
        /// </summary>
		public KvtRepeater()
		{
		}

		/// <summary>
        /// Gets a <see cref="T:System.Web.UI.ControlCollection"></see> object that represents the child controls for a specified server control in the UI hierarchy.
        /// </summary>
        /// <value></value>
        /// <returns>The collection of child controls for the specified server control.</returns>
		public override ControlCollection Controls
		{
			get
			{
				this.EnsureChildControls();
				return base.Controls;
			}
		}

		private ITemplate m_headerTemplate;
		/// <summary>
        /// Gets or sets the header template.
        /// </summary>
        /// <value>The header template.</value>
		[Browsable(false)]
		[TemplateContainer(typeof(KvtItem))]
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		public ITemplate HeaderTemplate
		{
			get { return m_headerTemplate; }
			set { m_headerTemplate = value; }
		}

		private ITemplate m_itemTemplate;
		/// <summary>
        /// Gets or sets the item template.
        /// </summary>
        /// <value>The item template.</value>
		[Browsable(false)]
		[TemplateContainer(typeof(KvtItem))]
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		public ITemplate ItemTemplate
		{
			get { return m_itemTemplate; }
			set { m_itemTemplate = value; }
		}
			

		private ITemplate m_altenateItemTemplate;
        /// <summary>
        /// Gets or sets the alternating item template.
        /// </summary>
        /// <value>The alternating item template.</value>
		[Browsable(false)]
		[TemplateContainer(typeof(KvtItem))]
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		public ITemplate AlternatingItemTemplate
		{
			get { return m_altenateItemTemplate; }
			set { m_altenateItemTemplate = value; }
		}

		private ITemplate m_footerTemplate;
        /// <summary>
        /// Gets or sets the footer template.
        /// </summary>
        /// <value>The footer template.</value>
		[Browsable(false)]
		[TemplateContainer(typeof(KvtItem))]
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		public ITemplate FooterTemplate
		{
			get { return m_footerTemplate; }
			set { m_footerTemplate = value; }
		}

        /// <summary>
        /// Gets or sets the data source ID.
        /// </summary>
        /// <value>The data source ID.</value>
		[Category("Data")]
		public virtual string DataSourceID
		{
			get
			{
				if (ViewState["DataSourceID"] == null)
				{
					return string.Empty;
				}
				return (string)ViewState["DataSourceID"];
			}
			set
			{
				ViewState["DataSourceID"] = value;
			}
		}

		//[Category("Data")]
		//public virtual string DataMember
		//{
		//    get
		//    {
		//        if (ViewState["DataMember"] == null)
		//        {
		//            return string.Empty;
		//        }
		//        return (string)ViewState["DataMember"];
		//    }
		//    set
		//    {
		//        ViewState["DataMember"] = value;
		//    }
		//}

		//private ProductDataSourceView m_currentView;

		System.Collections.IEnumerable m_currentView;
        /// <summary>
        /// Connects to data source view.
        /// </summary>
        /// <returns>an IEnumerable of DataSourceView</returns>
		private System.Collections.IEnumerable ConnectToDataSourceView()
		{
			if (m_currentView == null)
			{
				SPL.Web.Data.KvtDataSource datasource = null;
				//First look in the naming container
            Control ctl = this.NamingContainer.FindControl(DataSourceID);
            if (ctl == null)
            {
               //if not found, look at the page level
               ctl = this.Page.FindControl(DataSourceID);
               if (ctl == null)
               {
                  throw new System.Web.HttpException("Datasource does not exists");
               }
            }
				datasource = ctl as SPL.Web.Data.KvtDataSource;
				if (datasource == null)
				{
					throw new System.Web.HttpException("Datasource must be data control");
				}
				
				System.Collections.IEnumerable dsv = datasource.GetEntityList(); //this.DataMember);
				if (dsv == null)
				{
					throw new System.Web.HttpException("View not found");
				}
				m_currentView = dsv;
			}
			return m_currentView;
		}

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
		protected override void CreateChildControls()
		{
			if (ChildControlsCreated)
			{
				return;
			}
			Controls.Clear();

			System.Collections.IEnumerable datas = (System.Collections.IEnumerable)ConnectToDataSourceView();

			if (datas != null)
			{
				if (m_headerTemplate != null)
				{
					Control headerItem = new Control();
					m_headerTemplate.InstantiateIn(headerItem);
					Controls.Add(headerItem);
				}

				int pos = 0;
				foreach (object o in datas)
				{
					SPL.Entities.Kvt entity = o as SPL.Entities.Kvt;
					KvtItem container = new KvtItem(entity);

					if (m_itemTemplate != null && (pos % 2) == 0)
					{
						m_itemTemplate.InstantiateIn(container);
					}
					else
					{
						if (m_altenateItemTemplate != null)
						{
							m_altenateItemTemplate.InstantiateIn(container);
						}
						else if (m_itemTemplate != null)
						{
							m_itemTemplate.InstantiateIn(container);
						}
						else
						{
							// no template !!!
						}
					}
					Controls.Add(container);
					pos++;
				}

				if (m_footerTemplate != null)
				{
					Control footerItem = new Control();
					m_footerTemplate.InstantiateIn(footerItem);
					Controls.Add(footerItem);
				}
				ChildControlsCreated = true;
			}
		}

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
		protected override void OnPreRender(EventArgs e)
		{
			base.DataBind();
		}

		#region Design time
        /// <summary>
        /// Renders at design time.
        /// </summary>
        /// <returns>a  string of the Designed HTML</returns>
		internal string RenderAtDesignTime()
		{			
			return "Designer currently not implemented"; 
		}

		#endregion
	}

    /// <summary>
    /// A wrapper type for the entity
    /// </summary>
	[System.ComponentModel.ToolboxItem(false)]
	public class KvtItem : System.Web.UI.Control, System.Web.UI.INamingContainer
	{
		private SPL.Entities.Kvt _entity;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:KvtItem"/> class.
        /// </summary>
		public KvtItem()
			: base()
		{ }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:KvtItem"/> class.
        /// </summary>
		public KvtItem(SPL.Entities.Kvt entity)
			: base()
		{
			_entity = entity;
		}
		
        /// <summary>
        /// Gets the Id
        /// </summary>
        /// <value>The Id.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Int64 Id
		{
			get { return _entity.Id; }
		}
        /// <summary>
        /// Gets the I1InputDate
        /// </summary>
        /// <value>The I1InputDate.</value>
		[System.ComponentModel.Bindable(true)]
		public System.DateTime I1InputDate
		{
			get { return _entity.I1InputDate; }
		}
        /// <summary>
        /// Gets the I9KhiVaoDoGppNhap
        /// </summary>
        /// <value>The I9KhiVaoDoGppNhap.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I9KhiVaoDoGppNhap
		{
			get { return _entity.I9KhiVaoDoGppNhap; }
		}
        /// <summary>
        /// Gets the I21XeBonFiqBupro
        /// </summary>
        /// <value>The I21XeBonFiqBupro.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I21XeBonFiqBupro
		{
			get { return _entity.I21XeBonFiqBupro; }
		}
        /// <summary>
        /// Gets the I34NhapCondBh
        /// </summary>
        /// <value>The I34NhapCondBh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I34NhapCondBh
		{
			get { return _entity.I34NhapCondBh; }
		}
        /// <summary>
        /// Gets the I35MeBhSoKctv
        /// </summary>
        /// <value>The I35MeBhSoKctv.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I35MeBhSoKctv
		{
			get { return _entity.I35MeBhSoKctv; }
		}
        /// <summary>
        /// Gets the I36NhapCondBhTuTk125
        /// </summary>
        /// <value>The I36NhapCondBhTuTk125.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I36NhapCondBhTuTk125
		{
			get { return _entity.I36NhapCondBhTuTk125; }
		}
        /// <summary>
        /// Gets the I37NhapLpgTuTk107
        /// </summary>
        /// <value>The I37NhapLpgTuTk107.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I37NhapLpgTuTk107
		{
			get { return _entity.I37NhapLpgTuTk107; }
		}
        /// <summary>
        /// Gets the I38Fqic1011
        /// </summary>
        /// <value>The I38Fqic1011.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I38Fqic1011
		{
			get { return _entity.I38Fqic1011; }
		}
        /// <summary>
        /// Gets the I39Fqic1012
        /// </summary>
        /// <value>The I39Fqic1012.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I39Fqic1012
		{
			get { return _entity.I39Fqic1012; }
		}
        /// <summary>
        /// Gets the I41XuatCondBhTheoMe
        /// </summary>
        /// <value>The I41XuatCondBhTheoMe.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I41XuatCondBhTheoMe
		{
			get { return _entity.I41XuatCondBhTheoMe; }
		}
        /// <summary>
        /// Gets the I42XuatCondBhLenhXuatSo
        /// </summary>
        /// <value>The I42XuatCondBhLenhXuatSo.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I42XuatCondBhLenhXuatSo
		{
			get { return _entity.I42XuatCondBhLenhXuatSo; }
		}
        /// <summary>
        /// Gets the I43MeTdXuatBupro
        /// </summary>
        /// <value>The I43MeTdXuatBupro.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I43MeTdXuatBupro
		{
			get { return _entity.I43MeTdXuatBupro; }
		}
        /// <summary>
        /// Gets the I44TankXuatBupro
        /// </summary>
        /// <value>The I44TankXuatBupro.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I44TankXuatBupro
		{
			get { return _entity.I44TankXuatBupro; }
		}
        /// <summary>
        /// Gets the I45GiamDinhXuatBupro
        /// </summary>
        /// <value>The I45GiamDinhXuatBupro.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I45GiamDinhXuatBupro
		{
			get { return _entity.I45GiamDinhXuatBupro; }
		}
        /// <summary>
        /// Gets the I46XuatKhacLpg
        /// </summary>
        /// <value>The I46XuatKhacLpg.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I46XuatKhacLpg
		{
			get { return _entity.I46XuatKhacLpg; }
		}
        /// <summary>
        /// Gets the I47XuatKhacCondBh
        /// </summary>
        /// <value>The I47XuatKhacCondBh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I47XuatKhacCondBh
		{
			get { return _entity.I47XuatKhacCondBh; }
		}
        /// <summary>
        /// Gets the I48XaDotLpg
        /// </summary>
        /// <value>The I48XaDotLpg.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I48XaDotLpg
		{
			get { return _entity.I48XaDotLpg; }
		}
        /// <summary>
        /// Gets the I49XaDotCondBh
        /// </summary>
        /// <value>The I49XaDotCondBh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I49XaDotCondBh
		{
			get { return _entity.I49XaDotCondBh; }
		}
        /// <summary>
        /// Gets the I52XaDotKahxntlpg
        /// </summary>
        /// <value>The I52XaDotKahxntlpg.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I52XaDotKahxntlpg
		{
			get { return _entity.I52XaDotKahxntlpg; }
		}
        /// <summary>
        /// Gets the I53XaDotKahxntCond
        /// </summary>
        /// <value>The I53XaDotKahxntCond.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I53XaDotKahxntCond
		{
			get { return _entity.I53XaDotKahxntCond; }
		}
        /// <summary>
        /// Gets the I54XuatCondBhTank
        /// </summary>
        /// <value>The I54XuatCondBhTank.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I54XuatCondBhTank
		{
			get { return _entity.I54XuatCondBhTank; }
		}
        /// <summary>
        /// Gets the I55XuatCondBhGiamDinh
        /// </summary>
        /// <value>The I55XuatCondBhGiamDinh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I55XuatCondBhGiamDinh
		{
			get { return _entity.I55XuatCondBhGiamDinh; }
		}
        /// <summary>
        /// Gets the I56TonCondBh
        /// </summary>
        /// <value>The I56TonCondBh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I56TonCondBh
		{
			get { return _entity.I56TonCondBh; }
		}
        /// <summary>
        /// Gets the I57TonBupro
        /// </summary>
        /// <value>The I57TonBupro.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I57TonBupro
		{
			get { return _entity.I57TonBupro; }
		}
        /// <summary>
        /// Gets the I58TenMeMncs
        /// </summary>
        /// <value>The I58TenMeMncs.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I58TenMeMncs
		{
			get { return _entity.I58TenMeMncs; }
		}
        /// <summary>
        /// Gets the I59VaoDoz3002
        /// </summary>
        /// <value>The I59VaoDoz3002.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I59VaoDoz3002
		{
			get { return _entity.I59VaoDoz3002; }
		}
        /// <summary>
        /// Gets the I60RaDoz7900
        /// </summary>
        /// <value>The I60RaDoz7900.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I60RaDoz7900
		{
			get { return _entity.I60RaDoz7900; }
		}
        /// <summary>
        /// Gets the I61MeMncsSoLift
        /// </summary>
        /// <value>The I61MeMncsSoLift.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I61MeMncsSoLift
		{
			get { return _entity.I61MeMncsSoLift; }
		}
        /// <summary>
        /// Gets the I62XuatMeMncs
        /// </summary>
        /// <value>The I62XuatMeMncs.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I62XuatMeMncs
		{
			get { return _entity.I62XuatMeMncs; }
		}
        /// <summary>
        /// Gets the I63TonCondNcsTaiKctv
        /// </summary>
        /// <value>The I63TonCondNcsTaiKctv.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I63TonCondNcsTaiKctv
		{
			get { return _entity.I63TonCondNcsTaiKctv; }
		}
        /// <summary>
        /// Gets the I64XuatCondNcsTheoTank
        /// </summary>
        /// <value>The I64XuatCondNcsTheoTank.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I64XuatCondNcsTheoTank
		{
			get { return _entity.I64XuatCondNcsTheoTank; }
		}
        /// <summary>
        /// Gets the I65XuatCondNcsTheoGiamDinh
        /// </summary>
        /// <value>The I65XuatCondNcsTheoGiamDinh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Double I65XuatCondNcsTheoGiamDinh
		{
			get { return _entity.I65XuatCondNcsTheoGiamDinh; }
		}
        /// <summary>
        /// Gets the I66SuKienVanHanh
        /// </summary>
        /// <value>The I66SuKienVanHanh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I66SuKienVanHanh
		{
			get { return _entity.I66SuKienVanHanh; }
		}
        /// <summary>
        /// Gets the I67NoteBcn
        /// </summary>
        /// <value>The I67NoteBcn.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I67NoteBcn
		{
			get { return _entity.I67NoteBcn; }
		}
        /// <summary>
        /// Gets the I68NoteQlsplbh
        /// </summary>
        /// <value>The I68NoteQlsplbh.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I68NoteQlsplbh
		{
			get { return _entity.I68NoteQlsplbh; }
		}
        /// <summary>
        /// Gets the I69NoteCondBhMe
        /// </summary>
        /// <value>The I69NoteCondBhMe.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I69NoteCondBhMe
		{
			get { return _entity.I69NoteCondBhMe; }
		}
        /// <summary>
        /// Gets the I70NoteGdme
        /// </summary>
        /// <value>The I70NoteGdme.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I70NoteGdme
		{
			get { return _entity.I70NoteGdme; }
		}
        /// <summary>
        /// Gets the I71NoteMncs
        /// </summary>
        /// <value>The I71NoteMncs.</value>
		[System.ComponentModel.Bindable(true)]
		public System.String I71NoteMncs
		{
			get { return _entity.I71NoteMncs; }
		}
        /// <summary>
        /// Gets the Status
        /// </summary>
        /// <value>The Status.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Int32 Status
		{
			get { return _entity.Status; }
		}
        /// <summary>
        /// Gets the InputUser
        /// </summary>
        /// <value>The InputUser.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Int32 InputUser
		{
			get { return _entity.InputUser; }
		}
        /// <summary>
        /// Gets the Hid
        /// </summary>
        /// <value>The Hid.</value>
		[System.ComponentModel.Bindable(true)]
		public System.Int64? Hid
		{
			get { return _entity.Hid; }
		}
        /// <summary>
        /// Gets the CreatedDate
        /// </summary>
        /// <value>The CreatedDate.</value>
		[System.ComponentModel.Bindable(true)]
		public System.DateTime CreatedDate
		{
			get { return _entity.CreatedDate; }
		}

        /// <summary>
        /// Gets a <see cref="T:SPL.Entities.Kvt"></see> object
        /// </summary>
        /// <value></value>
        [System.ComponentModel.Bindable(true)]
        public SPL.Entities.Kvt Entity
        {
            get { return _entity; }
        }
	}
}
